
var http = require('http')


var fs = require('fs')

var url = require('url')

var app = http.createServer(function(req,res){
    //1. 获取地址栏的路径
    // console.log(req.url); // /b.html
    // console.log(req.pathname)
    
    // req.url   http://127.0.0.1:8080/a.html?page=15&sex=boy
    var obj = url.parse(req.url, true)
    //obj
    
    // obj {
//   protocol: null,
//   slashes: null,
//   auth: null,
//   host: null,
//   port: null,
//   hostname: null,
//   hash: null,
//   search: '?page=15&sex=boy',
//   query: [Object: null prototype] { page: '15', sex: 'boy' },
//   pathname: '/a.html',
//   path: '/a.html?page=15&sex=boy',
//   href: '/a.html?page=15&sex=boy' 
// }

  

    console.log(obj)

    var filePath = '.'+ obj.pathname   // ./a.html
    // 请求的资源路径
    console.log(filePath)
    // get参数 sex的值
    console.log(obj.query.sex)
    // get参数 page的值
    console.log(obj.query.page)

    fs.readFile(filePath,function(err, data){
        if(err){
            res.write('page not found')
            res.end()
        }else{
            res.write(data)
            res.end()
        }
       
    })
    
})

app.listen(8080)